/*---------------------------------------------------------------------------*
 * Copyright (C) 2012 Ryan M. Murphy <ryan.m.murphy.77@gmail.com>
 *
 * File Name   : usr_wiFly.c
 *
 * Project     : Watts-rock
 *               <http://code.google.com/p/watts-rock-motherboard/>
 *
 * Description : This file is responsible for providing access to the
 *               Roving Networks Wifly module.
 *
 * Last Update : July, 9, 2013
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*
 * Include Files
 *---------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "arb_error.h"
#include "arb_thread.h"
#include "arb_semaphore.h"
#include "arb_device.h"
#include "arb_sysTimer.h"
#include "arb_printf.h"
#include "drv_wifly.h"
#include "usr_bms.h"

/*---------------------------------------------------------------------------*
 * Private Defines
 *---------------------------------------------------------------------------*/
#define WIFLY_SLEEP_TICKS (ARB_TICKS_PER_SECOND >> 1)

/*---------------------------------------------------------------------------*
 * The thread update rate in system timer ticks
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*
 * Private Typedefs
 *---------------------------------------------------------------------------*/
typedef struct
{
   /*------------------------------------------------------------------------*
    * Handle to the BMS driver.
    *------------------------------------------------------------------------*/
   t_DEVHANDLE t_wiflyHndl;
   /*------------------------------------------------------------------------*
    * Handle to the console thread
    *------------------------------------------------------------------------*/
   t_THRDHANDLE t_wiflyThread;

}t_wiFlyObjct;

/*---------------------------------------------------------------------------*
 * Private Function Prototypes
 *---------------------------------------------------------------------------*/
static void usr_wiFly( void);

/*---------------------------------------------------------------------------*
 * Private Global Variables
 *---------------------------------------------------------------------------*/
static t_wiFlyObjct gt_wiFlyObjct;

/*---------------------------------------------------------------------------*
 * Private Function Definitions
 *---------------------------------------------------------------------------*/
static void usr_wiFly( void)
{
   t_bmsMeasStatus t_bmsMeas;
   int8_t ac_buff[120];
   uint16_t s_size;

   /*------------------------------------------------------------------------*
    * Take the WiFly module out of reset, and put it in a known state.
    *------------------------------------------------------------------------*/
   arb_ioctl( gt_wiFlyObjct.t_wiflyHndl,
              WIFLY_BEGIN,
              0);

   while(RUN_FOREVER)
   {
      t_bmsMeas = usr_getBmsMeas();
      
      /*------------------------------------------------------------------------*
       * If enabled, send debug to the console...
       *------------------------------------------------------------------------*/
      s_size = sprintf(( char *)ac_buff, "Volts %2.2f Amps %2.3f Fuel %3.4f Temp %3.1f State %d\n\r",
      (double)t_bmsMeas.t_meas.f_volts,
      (double)t_bmsMeas.t_meas.f_avrCurrent,
      (double)t_bmsMeas.f_gasGuage,
      (double)t_bmsMeas.t_meas.f_temp,
      t_bmsMeas.t_state);

      arb_write( gt_wiFlyObjct.t_wiflyHndl,
                 ac_buff,
                 s_size);

      /*---------------------------------------------------------------------*
       * Allow other threads to run...
       *---------------------------------------------------------------------*/
      arb_sleep( WIFLY_SLEEP_TICKS);
   
   }/*End while(RUN_FOREVER)*/
   
}/*End usr_wiFly*/

/*---------------------------------------------------------------------------*
 * Public Function Definitions
 *---------------------------------------------------------------------------*/
t_error usr_wiFlyInit( void)
{

   /*------------------------------------------------------------------------*
    * Initialize the HMI object...
    *------------------------------------------------------------------------*/
   memset( (void *)&gt_wiFlyObjct, 0, sizeof( t_wiFlyObjct));

   /*------------------------------------------------------------------------*
    * Create a new thread.
    *------------------------------------------------------------------------*/
   gt_wiFlyObjct.t_wiflyThread = arb_threadCreate( usr_wiFly,
                                                   0,
                                                   0,
                                                   ARB_STACK_384B,
                                                   2);

   if( gt_wiFlyObjct.t_wiflyThread < 0)
   {
      return gt_wiFlyObjct.t_wiflyThread;
   }/*End if( gt_wiFlyObjct.t_wiflyThread < 0)*/

   /*------------------------------------------------------------------------*
    * Open a handle to the BMS driver.
    *------------------------------------------------------------------------*/
   gt_wiFlyObjct.t_wiflyHndl = arb_open( "wiflyDevice0",
                                         ARB_O_READ |
                                         ARB_O_WRITE);

   if( gt_wiFlyObjct.t_wiflyHndl < 0)
   {
      return gt_wiFlyObjct.t_wiflyHndl;
   }

   return ARB_PASSED;

}/*End usr_wifiInit*/